package com.gz.jdbc.datasource;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.jupiter.api.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;

public class Druid_ {//推荐使用druid,c3p0也用因为是老牌技术，但druid更稳定，速度快

@Test
    public void testdruid_() throws Exception {
        //1.加入druid jar包
        //2.加入配置文件druid.properties，拷贝到项目的src目录下
        //3.创建Properties对象，读取配置文件
        Properties properties=new Properties();
        properties.load(new FileInputStream("src\\druid.properties"));
        //4.创建一个指定参数的数据库连接池
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        long start = System.currentTimeMillis();
        for (int i = 0; i < 5000000; i++) {
            Connection connection = dataSource.getConnection();
//            System.out.println("连接成功");
            connection.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("druid 连接5000000次musql耗时: "+(end - start)); //耗时3023ms
                                                                        //比c3p07169ms快

    }
}
